<?php
/*  LOAD SYSTEM FUNCTIONS MANUALLY
    ============================================================================================================== */
	require_once( "functions/inc.database.php" );
	require_once( "functions/inc.getData.php" );
	require_once( "functions/inc.generalFunctions.php" );
	require_once( "functions/inc.pageSetup.php" );
	
	# init session
	session_start();
	
	page_header( "Wayne Schlegel Inc. | Registration", false );
	
	if( empty( $_POST ) ){
		$_POST['gender']               = "";
		$_POST['firstName']            = "";
		$_POST['lastName']             = "";
		$_POST['email']                = "";
		$_POST['url']                  = "";
		$_POST['street']               = "";
		$_POST['postCode']             = "";
		$_POST['city']                 = "";
		$_POST['primaryPhoneNumber']   = "";
		$_POST['secondaryPhoneNumber'] = "";
		$_POST['mobilePhoneNumber']    = "";
		$_POST['faxNumber']            = "";
		$_POST['countryCode']          = "";
		$_POST['languageCode']         = "";
		$_POST['username']             = "";
		$_POST['password']             = "";
	}
?>

<?php
	$errorCount      = 0;
	$errorUserExists = 0;

	if( !empty( $_POST['send'] ) && $_POST['send'] == "register" ) {
		// check for mandatory fields
		if( empty( $_POST['gender'] ) )       { $errorCount++; }
		if( empty( $_POST['firstName'] ) )    { $errorCount++; }
		if( empty( $_POST['lastName'] ) )     { $errorCount++; }
		if( empty( $_POST['email'] ) )        { $errorCount++; }
		if( empty( $_POST['countryCode'] ) )  { $errorCount++; }
		if( empty( $_POST['languageCode'] ) ) { $errorCount++; }
		if( empty( $_POST['username'] ) )     { $errorCount++; }
		if( empty( $_POST['password'] ) )     { $errorCount++; }
		
		// check if user exists already
		$sql = "SELECT id FROM userData WHERE username = '" . $_POST['username'] . "'";
		$res = mysql_query( $sql );
		if( mysql_num_rows( $res ) ) {
			$errorUserExists++;
		}
		
		if( $errorCount == 0 ) {
			// save new user
			$now       = date( "Y-m-d H:i:s" );
			$unique_id = uniqueId();
			
			$sql   = "INSERT INTO contactData (gender, firstName, lastName, email, url, street, postCode, city, primaryPhoneNumber, secondaryPhoneNumber, mobilePhoneNumber, faxNumber, countryCode, languageCode, lastWriteAccess)";
			$sql  .= " VALUES ('" . $_POST['gender'] . "', '" . $_POST['firstName'] . "', '" . $_POST['lastName'] . "', '" . $_POST['email'] . "', '" . $_POST['url'] . "', '" . $_POST['street'] . "', '" . $_POST['postCode'] . "', '" . $_POST['city'] . "', '" . $_POST['primaryPhoneNumber'] . "', '" . $_POST['secondaryPhoneNumber'] . "', '" . $_POST['mobilePhoneNumber'] . "', '" . $_POST['faxNumber'] . "', '" . $_POST['countryCode'] . "', '" . $_POST['languageCode'] . "', '" . $now . "');";
			$res = mysql_query( $sql );
			if( !$res ) {
				echo "ERROR while saving contactData | " . mysql_error();
			}
			$contactData_id = mysql_insert_id();
			
			$sql2  = "INSERT INTO userData (contactData_id, username, password, timeOfRegistration, hasBeenActivated, activationId, lastWriteAccess) ";
			$sql2 .= "VALUES ('" . $contactData_id . "', '" . $_POST['username'] . "', '" . $_POST['password'] . "', '" . $now . "', 0, '" . $unique_id . "', '" . $now . "')";
			$res2  = mysql_query( $sql2 );
			if( !$res2 ) {
				echo "ERROR while saving userData | " . mysql_error();
			}
			
			if( $res && $res2 ) {
				// send mail with activation link
				$mailtext  = "Dear " . $_POST['firstName'] . " " . $_POST['lastName'] . ", \n";
				$mailtext .= "we received your registration for Wayne Schlegel Inc. and you're nearly finished with your registrattion. Ther's only one more step to activate your account by clicking the following link: \n";
				$mailtext .= "http://" . $_SERVER['SERVER_NAME'] . "/activateUser.php?uid=" . $unique_id . "&usr=" . base64_encode( $_POST['username'] );
				$headers = 'From: registration@wayne-schlegel.com' . "\r\n" . 'Reply-To: cmd@stefanmoeller.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
				mail( $_POST['email'], "[Wayne Schlegel Inc.] Your registration: account '" . $_POST['username'] . "' needs activation.", $mailtext, $headers );
					echo "<p>Your account has been created. You will receive an e-mail to " . $_POST['email'] . " with your activation-link. Before activating your account you're not able to login. Please check your mail-account.</p>";
			}
		}
	}
	
	if( empty( $_POST['send'] ) || $_POST['send'] != "register" || $errorCount > 0 || $errorUserExists > 0 ) {
?>
	<h1>Registration</h1>
	<?php
		if( $errorCount >= 1 ){
			echo "<p class=\"error\">Please fill at least all mandatory fields (marked with an astersik).</p>";
		}
		if( $errorUserExists >= 1 ){
			echo "<p class=\"error\">The user already exists.</p>";
		}
	?>
	<form method="post" action="" class="iForm formTable">
		<select name="gender">
			<option value=""      >&mdash; select your gender * &mdash;</option>
			<option value="MALE"  <?php if( $_POST['gender'] == "MALE" )   echo "selected=\"selected\" "; ?>>male</option>
			<option value="FEMALE"<?php if( $_POST['gender'] == "FEMALE" ) echo "selected=\"selected\" "; ?>>female</option>
		</select>
		<label class="placeholder" for="firstName">first name *</label>
		<input type="text" name="firstName"            value="<?=$_POST['firstName']?>"            autocorrect="off" title=""  />
		<label class="placeholder" for="lastName">last name *</label>
		<input type="text" name="lastName"             value="<?=$_POST['lastName']?>"             autocorrect="off" title=""  />
		<label class="placeholder" for="email">e-mail *</label>
		<input type="text" name="email"                value="<?=$_POST['email']?>"                autocorrect="off" title=""  />
		<label class="placeholder" for="url">homepage</label>
		<input type="text" name="url"                  value="<?=$_POST['url']?>"                  autocorrect="off" title=""  />
		<label class="placeholder" for="street">street</label>
		<input type="text" name="street"               value="<?=$_POST['street']?>"               autocorrect="off" title=""  />
		<label class="placeholder" for="postCode">post code</label>
		<input type="text" name="postCode"             value="<?=$_POST['postCode']?>"             autocorrect="off" title=""  />
		<label class="placeholder" for="city">city</label>
		<input type="text" name="city"                 value="<?=$_POST['city']?>"                 autocorrect="off" title=""  />
		phone numbers<br />
		<label class="placeholder" for="primaryPhoneNumber">primary</label>
		<input type="text" name="primaryPhoneNumber"   value="<?=$_POST['primaryPhoneNumber']?>"   autocorrect="off" title=""  />
		<label class="placeholder" for="secondaryPhoneNumber">secondary</label>
		<input type="text" name="secondaryPhoneNumber" value="<?=$_POST['secondaryPhoneNumber']?>" autocorrect="off" title=""  />
		<label class="placeholder" for="mobilePhoneNumber">mobile</label>
		<input type="text" name="mobilePhoneNumber"    value="<?=$_POST['mobilePhoneNumber']?>"    autocorrect="off" title=""  />
		<label class="placeholder" for="faxNumber">fax number</label>
		<input type="text" name="faxNumber"            value="<?=$_POST['faxNumber']?>"            autocorrect="off" title=""  />
		<select name="countryCode" title="">
			<option value=""      >&mdash; select your country * &mdash;</option>
			<option value="BE" <?php if( $_POST['countryCode'] == "BE" ) echo "selected=\"selected\""; ?>>Belgium</option>
			<option value="DE" <?php if( $_POST['countryCode'] == "DE" ) echo "selected=\"selected\""; ?>>Germany</option>
			<option value="NL" <?php if( $_POST['countryCode'] == "NL" ) echo "selected=\"selected\""; ?>>The Netherlands</option>
		</select>
		<select name="languageCode" title="">
			<option value="">&mdash; select your preferred language * &mdash;</option>
			<option value="EN" <?php if( $_POST['languageCode'] == "EN" ) echo "selected=\"selected\""; ?>>english</option>
		</select><br />
		<br />
		<label class="placeholder" for="username">username *</label>
		<input type="text" name="username"             value="<?=$_POST['username']?>"             autocorrect="off" title=""  />
		<label class="placeholder" for="password">password *</label>
		<input type="password" name="password"             value="<?=$_POST['password']?>"             autocorrect="off" title=""  />

		<input type="submit" name="send" value="register" />

	</form>
<?php 
	} 
?>
	
	<table class="iTable">
		<tr class="moreInformation lastItem">
			<td>
				<a href="loginForm.php">back to Login</a>
			</td>
		</tr>
	</table>
</body>
</html>